package org.easyupdater;

import java.io.InputStream;
import java.io.OutputStream;

import org.easyupdater.model.ApplicationSpec;
import org.easyupdater.model.BundleSpec;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**
 * An XStream serializer implementation
 * 
 * @author Bogdan Dimitriu
 */
public class XStreamApplicationSpecSerializer implements IApplicationSpecSerializer {

	private final XStream xstream;

	public XStreamApplicationSpecSerializer() {
		/*
		 * ATTENTION ! XStream is used in this case as a thread safe object. If
		 * the annotation auto-detection for XStream is turned on this will not
		 * hold true any more
		 */
		xstream = new XStream(new DomDriver());
		xstream.processAnnotations(ApplicationSpec.class);
		xstream.processAnnotations(BundleSpec.class);
	}

	@Override
	public ApplicationSpec downloadApplicationSpec(InputStream appUpdateSiteInputStream) {
		ApplicationSpec applicationSpec = (ApplicationSpec) xstream
				.fromXML(appUpdateSiteInputStream);
		return applicationSpec;
	}

	@Override
	public void saveApplicationSpec(ApplicationSpec applicationSpec, OutputStream outputStream) {
		xstream.toXML(applicationSpec, outputStream);
	}
}
